package es.udc.cameraar2;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.google.gson.Gson;

public class ThreadGetData extends Thread{

		
	    private boolean run = false;
	    private String url;    
	    
	 
	    public ThreadGetData(String text) {
	    	this.url = text;
	    }

	    public void setRunning(boolean run) {
	        this.run = run;
	    }
	    
	    public boolean isRunning() {
	        return this.run;
	    }
	    
	    
	 
	    @Override
	    public synchronized void run() {       		    
	            try {
	            	Gson gson = new Gson();
	                Reader r = new InputStreamReader(getJSONData(url));
	                Log.i("MY INFO", r.toString());
	                Container objs = gson.fromJson(r, Container.class);
	                Log.i("MY INFO", ""+objs.getTrends().size());	                
	                for(Node tr : objs.getTrends()){	                	   	                     	                	                       	                       	            
	                
	                }
	            	                	                	
	            }
	            catch(Exception e){}	            
	    }
	    
	    
	    public InputStream getJSONData(String url){
	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        URI uri;
	        InputStream data = null;
	        try {
	            uri = new URI(url);
	            HttpGet method = new HttpGet(uri);
	            HttpResponse response = httpClient.execute(method);
	            data = response.getEntity().getContent();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        
	        return data;
	    }
		
	}
